/*
Copyright 2013 ToysSamurai LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

;(function ( $, window, document, undefined ) {
    $.widget( "toyssamurai.ribbonmenu" , {

        //Options to be used as defaults
        options: {
          container: "#document",
          trigger: "#ribbon-menu-trigger",
          menu: {
            id: "ribbon-menu",
            class: "ribbon-menu"
          },
          expandedClass: "expanded",
          animation: {left: 100}
        },

        //Setup widget (eg. element creation, apply theming
        // , bind events etc.)
        _create: function () {
          var settings = this.options;
          this.$sourceMenu = $(this.element);
          this.$trigger = $(settings.trigger);
          this.$container = $(settings.container);
          this.$sourceMenu.clone ().attr (settings.menu).appendTo ($(document.body));
          this.$trigger.click(this, this._click);
        },

        // Destroy an instantiated plugin and clean up 
        // modifications the widget has made to the DOM
        destroy: function () {

            // this.element.removeStuff();
            // For UI 1.8, destroy must be invoked from the 
            // base widget
            $.Widget.prototype.destroy.call(this);
            // For UI 1.9, define _destroy instead and don't 
            // worry about 
            // calling the base widget
        },

        _click: function ( e ) {
          e.preventDefault ();
          e.data.click ();
        },

        click: function () {
          var settings = this.options;
          if (this.$trigger.hasClass (this.options.expandedClass)) {
            this.$trigger.removeClass (this.options.expandedClass);
            this.$container.css ({position: "relative"}).animate ({
              left: 0
            });
          }
          else {
            this.$trigger.addClass (this.options.expandedClass);
            this.$container.css ({position: "relative"}).animate (this.options.animation);
          }
        },

        // Respond to any changes the user makes to the 
        // option method
        _setOption: function ( key, value ) {
            switch (key) {
              case "someValue":
                  //this.options.someValue = doSomethingWith( value );
                  break;
              default:
                  //this.options[ key ] = value;
                  break;
            }

            // For UI 1.8, _setOption must be manually invoked 
            // from the base widget
            $.Widget.prototype._setOption.apply( this, arguments );
            // For UI 1.9 the _super method can be used instead
            // this._super( "_setOption", key, value );
        }
    });

})( jQuery, window, document );